﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Diagnostics;
using System.Globalization;

namespace Hallenberg.Web
{
    internal static class MembershipExtensions
    {
        private static Dictionary<string, string> s_UserPasswords = new Dictionary<string, string>();
        private static Dictionary<string, string> s_UserPasswordQuestions = new Dictionary<string, string>();

        internal static MembershipUserCollection ToCollection(this List<MembershipUser> source)
        {
            MembershipUserCollection collection = new MembershipUserCollection();
            foreach (MembershipUser user in source)
            {
                collection.Add(user);
            }

            return collection;
        }

        internal static void SetPassword(this MembershipUser user, string password)
        {
            Debug.Assert(!string.IsNullOrEmpty(password));
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            if (!s_UserPasswords.ContainsKey(user.UserName))
            {
                s_UserPasswords.Add(user.UserName, password);
            }
            else
            {
                s_UserPasswords[user.UserName] = password;
            }
        }

        internal static string GetPasswordFromMemory(this MembershipUser user)
        {
            Debug.Assert(s_UserPasswords.ContainsKey(user.UserName));
            if (!s_UserPasswords.ContainsKey(user.UserName))
            {
                throw new InvalidOperationException("User has no associated password");
            }

            return s_UserPasswords[user.UserName];
        }

        internal static void SetPasswordAnswer(this MembershipUser user, string passwordAnswer)
        {
            if (!s_UserPasswordQuestions.ContainsKey(user.UserName))
            {
                s_UserPasswordQuestions.Add(user.UserName, passwordAnswer);
            }
            else
            {
                s_UserPasswordQuestions[user.UserName] = passwordAnswer;
            }
        }

        internal static string GetPasswordAnswer(this MembershipUser user)
        {
            Debug.Assert(s_UserPasswordQuestions.ContainsKey(user.UserName));
            if (!s_UserPasswordQuestions.ContainsKey(user.UserName))
            {
                throw new InvalidOperationException("MembershipUser does not have any associated password answers");
            }

            return s_UserPasswordQuestions[user.UserName];
        }

        internal static string GetLoweredPasswordAnswer(this MembershipUser user)
        {
            Debug.Assert(s_UserPasswordQuestions.ContainsKey(user.UserName));
            if (!s_UserPasswordQuestions.ContainsKey(user.UserName))
            {
                throw new InvalidOperationException("MembershipUser does not have any associated password answers");
            }

            return s_UserPasswordQuestions[user.UserName].ToLower(CultureInfo.CurrentCulture);
        }
    }
}
